<template>
  <div class="app-container">
    <el-table border :data="detailList" style="width: 100%">
      <el-table-column label="编号" width="100" type="index" align="center" fixed="left"
                       :index="index=>index+1"/>

      <el-table-column prop="resourceName" label="食材名称" align="center" width="180"/>
      <el-table-column prop="planAmount" label="计划数量" align="center" width="180"/>
      <el-table-column prop="amount" label="实际数量" align="center" width="180">
        <template #default="scope">
          <span v-show="!scope.row.editFlag">{{scope.row.amount}}</span>
          <el-input v-show="scope.row.editFlag"
                    v-model="scope.row.amount">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column prop="unit" label="单位" align="center" width="180"/>
      <el-table-column prop="price" label="单价" align="center" width="180">
        <template #default="scope">
          <span v-show="!scope.row.editFlag">{{scope.row.price}}</span>
          <el-input v-show="scope.row.editFlag"
                    v-model="scope.row.price">
          </el-input>
        </template>
      </el-table-column>
<!--      <el-table-column prop="status" label="是否入库" align="center" width="180">-->
<!--        <template #default="scope">-->
<!--          <span v-show="!scope.row.editFlag">{{scope.row.status}}</span>-->
<!--          <el-select v-show="scope.row.editFlag" v-model="scope.row.status" placeholder="请选择" clearable :style="{width: '100%'}">-->
<!--            <el-option v-for="(item, index) in statusOptions" :key="index" :label="item.label"-->
<!--                       :value="item.value" :disabled="item.disabled"></el-option>-->
<!--          </el-select>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column prop="direct" label="是否直接出库" align="center" width="180">
        <template #default="scope">
          <span v-show="!scope.row.editFlag">{{scope.row.direct}}</span>
          <el-select v-show="scope.row.editFlag" v-model="scope.row.direct" placeholder="请选择" clearable :style="{width: '100%'}">
            <el-option label="是" value="是"></el-option>
            <el-option label="否" value="否"></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="createBy" width="160" align="center"/>
      <el-table-column label="更新人" prop="updateBy" width="160" align="center"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" fixed="right" width="160">
        <template #default="scope">
          <div style="">
            <el-button size="small"
                       type="text"
                       icon="CircleCheck"
                       v-show="scope.row.editFlag"
                       :disabled="isIn"
                       @click="submit(scope.row, $index)">保存
            </el-button>
            <el-button size="small"
                       type="text"
                       icon="CircleClose"
                       v-show="scope.row.editFlag"
                       :disabled="isIn"
                       @click="cancel(scope.row, $index)">取消
            </el-button>
            <el-button size="small"
                       icon="Edit"
                       type="text"
                       v-show="!scope.row.editFlag"
                       v-if="!isIn"
                       @click="edit(scope.row, $index)"> {{editMessage}}
            </el-button>
            <el-button size="small"
                       icon="Edit"
                       type="text"
                       v-show="!scope.row.editFlag"
                       v-if="!isIn"
                       @click="del(scope.row, $index)"> 删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>


<script>
import { listAll, add, update, del } from "@/api/store/in/detail";
import { get as getStoreInInfo } from "@/api/store/in/in";
export default {
  name: "BaseDetail",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      id: undefined,
      // 遮罩层
      loading: true,
      isIn: false,
      editMessage: "编辑",
      // 菜品明细表格数据
      detailList: [],
      resourceOptions: [],
      statusOptions: [{
        label: "是",
        value: "是"
      },{
        label: "否",
        value: "否"
      }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false
    };
  },
  created() {
    this.id = this.$route.params && this.$route.params.id;
    getStoreInInfo(this.id).then(response => {
      if (response.data.status === "已入库") {
        this.isIn = true;
        this.editMessage = "已入库不能编辑";
      }
    });
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listAll({
        inId: this.id,
      }).then(response => {
          this.detailList = response.rows;
          this.loading = false;
        }
      );
    },
    addCommand: function() {
      var detail = {
        inId: this.id
      }
      add(detail).then(res => {
        this.getList();
      })
    },
    submit: function(row, index) {
      let updateData = {
        id: row.id,
        amount: row.amount,
        status: row.status,
        price: Number(row.price),
        direct: row.direct,
      }
      update(updateData).then(res => {
        this.$modal.msgSuccess("更新成功");
        row.editFlag = false;
        this.getList();
      })
    },
    cancel: function(row, index) {
      row.editFlag = false;
      this.getList();
    },
    edit: function(row, index) {
      row.editFlag = true;
      this.$set(this.detailList, index, row);
    },
    del: function(row, index) {
      del(row.id).then(response => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      });
    }
  }
};
</script>
